/**
 * 
 */
package sudoku.ui;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import sudoku.util.Config;
import sudoku.util.Language;

/**
 * @author Romet
 * 
 */
public class Symmetry implements ActionListener {

	private volatile SudokuUI m2ng;
	private volatile Config config;
	private volatile Language language;

	private JCheckBox symma;
	private JCheckBox symm90;
	private JCheckBox symm180;
	private JCheckBox symm270;
	private JCheckBox symmvert;
	private JCheckBox symmdiaga;
	private JCheckBox symmhor;
	private JCheckBox symmdiagm;

	private JButton ok;
	private JButton cancel;
	
	private JDialog dialog;

	public Symmetry(SudokuUI m2ng, Config config, Language language) {
		this.m2ng = m2ng;
		this.config = config;
		this.language = language;
		drawWindow();
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource().equals(ok)){
			if (symma.isSelected() || symm90.isSelected() || symm180.isSelected() || symm270.isSelected()
					|| symmvert.isSelected() || symmdiaga.isSelected() || symmhor.isSelected() || symmdiagm.isSelected()) {
				save();
				dialog.dispose();
			}
			else {
				JOptionPane.showMessageDialog(m2ng, language.getSymmetry_error(), language.getMessage(), JOptionPane.INFORMATION_MESSAGE);
			}
		}
		else if (e.getSource().equals(cancel)) {
			dialog.dispose();
		}
		else {
			if (symma.isSelected()) {
				symm90.setEnabled(false);
				symm180.setEnabled(false);
				symm270.setEnabled(false);
				symmvert.setEnabled(false);
				symmdiaga.setEnabled(false);
				symmhor.setEnabled(false);
				symmdiagm.setEnabled(false);
			} else {
				symm90.setEnabled(true);
				symm180.setEnabled(true);
				symm270.setEnabled(true);
				symmvert.setEnabled(true);
				symmdiaga.setEnabled(true);
				symmhor.setEnabled(true);
				symmdiagm.setEnabled(true);
			}
		}
	}

	public void drawWindow() {
		dialog = new JDialog(m2ng, language.getSudoku_symm(), true);
		dialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		JPanel panel = new JPanel(new GridLayout(8, 1));
		symma = new JCheckBox(language.getSymmetry_symma());
		symma.setSelected(config.isSymma());
		panel.add(symma);
		symma.addActionListener(this);
		symm90 = new JCheckBox(language.getSymmetry_symm90());
		symm90.setSelected(config.isSymm90());
		panel.add(symm90);
		symm180 = new JCheckBox(language.getSymmetry_symm180());
		symm180.setSelected(config.isSymm180());
		panel.add(symm180);
		symm270 = new JCheckBox(language.getSymmetry_symm270());
		symm270.setSelected(config.isSymm270());
		panel.add(symm270);
		symmvert = new JCheckBox(language.getSymmetry_symmvert());
		symmvert.setSelected(config.isSymmvert());
		panel.add(symmvert);
		symmdiaga = new JCheckBox(language.getSymmetry_symmdiaga());
		symmdiaga.setSelected(config.isSymmdiaga());
		panel.add(symmdiaga);
		symmhor = new JCheckBox(language.getSymmetry_symmhor());
		symmhor.setSelected(config.isSymmhor());
		panel.add(symmhor);
		symmdiagm = new JCheckBox(language.getSymmetry_symmdiagm());
		symmdiagm.setSelected(config.isSymmdiagm());
		panel.add(symmdiagm);
		if (symma.isSelected()) {
			symm90.setEnabled(false);
			symm180.setEnabled(false);
			symm270.setEnabled(false);
			symmvert.setEnabled(false);
			symmdiaga.setEnabled(false);
			symmhor.setEnabled(false);
			symmdiagm.setEnabled(false);
		}
		JPanel p = new JPanel(new GridLayout(1, 2));
		ok = new JButton("OK");
		ok.addActionListener(this);
		cancel = new JButton(language.getButton_cancel());
		cancel.addActionListener(this);

		p.add(ok);
		p.add(cancel);
		dialog.add(panel, BorderLayout.CENTER);
		dialog.add(p, BorderLayout.SOUTH);
		dialog.pack();
		dialog.setLocation(m2ng.getLocation().x+m2ng.getWidth()/2-dialog.getWidth()/2, m2ng.getLocation().y+m2ng.getHeight()/2-dialog.getHeight()/2);
		dialog.setVisible(true);
	}

	public void save() {
		config.setSymma(symma.isSelected());
		config.setSymm90(symm90.isSelected());
		config.setSymm270(symm270.isSelected());
		config.setSymm180(symm180.isSelected());
		config.setSymmvert(symmvert.isSelected());
		config.setSymmdiaga(symmdiaga.isSelected());
		config.setSymmhor(symmhor.isSelected());
		config.setSymmdiagm(symmdiagm.isSelected());
		config.salvesta();
		config.nulliGenereeritud();
	}
}
